﻿using Amazon.DynamoDBv2.DataModel;

namespace Dynamodb.xUnit
{
    public class RecentSuccessOrder
    {
        [DynamoDBHashKey]
        public long CustomerId { get; set; }

        [DynamoDBProperty]
        public long IBCContactId { get; set; }

        [DynamoDBProperty]
        public long? PurchaserContactId { get; set; }

        [DynamoDBRangeKey]
        public string? OrderId { get; set; }

        [DynamoDBProperty]
        public short IBCHomeLocation { get; set; }

        [DynamoDBProperty]
        public DateTime OrderDate { get; set; }

        [DynamoDBProperty]
        public DateTime InsertDate { get; set; }

        [DynamoDBProperty]
        public int TimeToLive { get; set; }

        /// <summary>
        /// 是否是纯rc订单
        /// </summary>
        /// <returns></returns>
        public bool IsCustomerOrder() => this.PurchaserContactId.HasValue == false;


        private bool PurchaserContactIdEquals(long? purchaserContactId)
        {
            return (!this.PurchaserContactId.HasValue
                && !purchaserContactId.HasValue)
                || (this.PurchaserContactId.HasValue
                && purchaserContactId.HasValue
                && this.PurchaserContactId.Value == purchaserContactId.Value);
        }

        public override bool Equals(object? obj)
        {
            if (obj == null) return false;
            var target = obj as RecentSuccessOrder;
            if (target == null) return false;

            if (this.CustomerId != target.CustomerId)
            {
                return false;
            }

            if (this.IBCContactId != target.IBCContactId)
            {
                return false;
            }

            if (!this.PurchaserContactIdEquals(target.PurchaserContactId))
            {
                return false;
            }

            if (this.OrderId != target.OrderId)
            {
                return false;
            }

            if (this.IBCHomeLocation != target.IBCHomeLocation)
            {
                return false;
            }

            if (this.OrderDate.EqualUpToSecond(target.OrderDate))
            {
                return false;
            }

            if (this.InsertDate.EqualUpToSecond(target.InsertDate))
            {
                return false;
            }

            if (this.TimeToLive != target.TimeToLive)
            {
                return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(this.CustomerId, this.IBCContactId, this.OrderId, this.IBCHomeLocation, this.InsertDate, this.OrderDate, this.TimeToLive, this.PurchaserContactId);
        }
    }
}
